const jwt = require('jsonwebtoken');
const response = require('../utils/response');
const { JWT_SECRET } = require('../config/jwt.config');

const verifyToken = (req, res, next) => {
  const token = req.headers.authorization?.split(' ')[1];

  if (!token) {
    return res.status(401).json(response(false, '未提供认证令牌', null, 401));
  }

  try {
    const decoded = jwt.verify(token, JWT_SECRET);
    req.user = decoded;
    next();
  } catch (error) {
    return res.status(401).json(response(false, '无效的认证令牌', null, 401));
  }
};
module.exports = verifyToken; 
